package com.example.kotlinview.coroutines

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

import com.example.kotlinview.R
import com.example.utils.ToastUtil
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.Main
import okhttp3.Dispatcher

class CoroutineAsyncActivity : AppCompatActivity() {
    lateinit var coroutineScope:CoroutineScope
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_coroutine_async)

         coroutineScope = CoroutineScope(Job() + Dispatchers.Main)

    }

    private fun launchAsync(){
        GlobalScope.launch {
            var deffer1 = async(Dispatchers.IO) {
                async1()
            }.await()
            var deffer2 = async(Dispatchers.IO) {
                async2()
            }.await()
            val result = deffer1 + deffer2
            println("the sum is: $result")
//            lineSharp()
        }
    }
    private suspend fun async1(): Int {
        delay(500)
        println("async1")
        return 1
    }
    private suspend fun async2(): Int {
        delay(1500)
        println("async2")
        return 2
    }
}